package com.stox.client.embedded.watchlist;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stox.client.api.watchlist.WatchlistEntryClient;
import com.stox.client.api.watchlist.event.WatchlistEntryAddedEvent;
import com.stox.services.watchlist.manager.WatchlistEntryManager;
import com.stox.shared.event.EventPublisher;
import com.stox.shared.model.WatchlistEntry;

@Component
public class EmbeddedWatchlistEntryClient implements WatchlistEntryClient {
	
	@Autowired
	private WatchlistEntryManager watchlistEntryManager;
	
	@Override
	public List<WatchlistEntry> findByWatchlistId(Long watchlistId) {
		return watchlistEntryManager.findByWatchlistId(watchlistId);
	}

	@Override
	public Long saveOrUpdate(WatchlistEntry watchlistEntry) {
		Long id = watchlistEntryManager.saveOrUpdate(watchlistEntry);
		watchlistEntry.setId(id);
		EventPublisher.publishEvent(new WatchlistEntryAddedEvent(this, watchlistEntry));
		return id;
	}

	@Override
	public void delete(Long id) {
		watchlistEntryManager.delete(id);
	}

}
